<template>
    <div>
        <h2>会员添加</h2>
        <table class="table table-bordered">
            <tr>
                <td>会员名称</td>
                <td>
                    <input type="text" v-model="state.Name">
                </td>
            </tr>

            <tr>
                <td>会员手机号</td>
                <td>
                    <input type="text" v-model="state.Phone">
                </td>
            </tr>

            <tr>
                <td>会员身份证号码</td>
                <td>
                    <input type="text" v-model="state.Card">
                </td>
            </tr>

            <tr>
                <td>会员的生日</td>
                <td>
                    <input type="date" v-model="state.Brithday">
                </td>
            </tr>

            <tr>
                <td>会员的性别</td>
                <td>
                    <input type="radio" name="sex" v-model="state.Sex" :value="1">男
                    <input type="radio" name="sex" v-model="state.Sex" :value="2">女
                </td>
            </tr>

            <tr>
                <td>会员的密码</td>
                <td>
                    <input type="password" v-model="state.Pwd">
                </td>
            </tr>

            <tr>
                <td>确认密码</td>
                <td>
                    <input type="password" v-model="Pwdok">
                </td>
            </tr>

            <tr>
                <td>会员的余额</td>
                <td>
                    <input type="text" v-model="state.Money">
                </td>
            </tr>

            <tr>
                <td>会员积分</td>
                <td>
                    <input type="text" v-model="state.JiFen">
                </td>
            </tr>

            <tr>
                <td>添加会员时间</td>
                <td>
                    <input type="date" v-model="state.CreateTime">
                </td>
            </tr>

            <tr>
                <td>会员的修改时间</td>
                <td>
                    <input type="date" v-model="state.UpdateTime">
                </td>
            </tr>

            <tr>
                <td></td>
                <td>
                    <input type="button" value="添加" @click="add">
                </td>
            </tr>
        </table>
    </div>
</template>

<script setup lang="ts">
import { reactive, toRefs, onMounted } from 'vue';
import { useRouter, useRoute } from 'vue-router';
import axios from 'axios';
let router = useRouter();
let route = useRoute();

let data: any = reactive({
    state: {
        "Name": "",
        "Phone": "",
        "Card": "",
        "Brithday": "",
        "Sex": 1, 
        "Pwd": "",
        "Money": "",
        "JiFen": "",
        "CreateTime": "",
        "UpdateTime": "",
    },
    Pwdok: "",
});
let { state, Pwdok } = toRefs(data);


const add = () => {
    if (state.value.Name == "") {
        alert("请输入姓名");
        return;
    }

    if (state.value.Phone == "") {
        alert("请输入手机号");
        return;
    }
    let reg = /^1[3-9]\d{9}$/;
    if (!reg.test(state.value.Phone)) {
        alert("请输入正确的手机号");
        return;
    }
    if (state.value.Card == "") {
        alert("请输入卡号");
        return;
    }
    let reg2 = /^\d{17}[0-9Xx]$/;
    if (!reg2.test(state.value.Card)) {
        alert("请输入正确的卡号");
        return;
    }

    if (state.value.Brithday == "") {
        alert("请输入生日");
        return;
    }
    if (state.value.Sex == "") {
        alert("请输入性别");
        return;
    }
    if (state.value.Pwd == "") {
        alert("请输入密码");
        return;
    }
    if (state.value.Pwdok == "") {
        alert("请输入确认密码");
        return;
    }
    if (state.value.Pwd != Pwdok.value) {
        alert("两次密码不一致");
        return;
    }
    if (state.value.Money == "") {
        alert("请输入余额");
        return;
    }

    if (state.value.JiFen == "") {
        alert("请输入积分");
        return;
    }
    if (isNaN(Number(state.value.JiFen)) == true) {
        alert("积分必须为数字");
        return;
    }



    let obj = {
        "Name": state.value.Name,
        "Phone": state.value.Phone,
        "Card": state.value.Card,
        "Brithday": state.value.Brithday,
        "Sex": state.value.Sex,
        "Pwd": state.value.Pwd,
        "Money": state.value.Money,
        "JiFen": state.value.JiFen,
        "CreateTime": state.value.CreateTime,
        "UpdateTime": state.value.UpdateTime,
    };
    axios({
        url: '/api/Member/Add',
        method: 'post',
        data: obj
    }).then(res => {
        if (res.data.data == 1) {
            alert("添加成功");
            return;
        }
        else if (res.data.data == -1) {
            alert("名字不能重复");
            return;
        }
        else if (res.data.data == -2) {
            alert("手机号不能重复");
            return;
        }
        else {
            alert("添加失败");
            return;
        }
    })
}

</script>

<style scoped></style>